/*
 * ClientClass.cpp
 *
 *  Created on: 2014-1-7
 *      Author: root
 */

#include "ClientClass.h"
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

namespace miniGameServer {

	ClientClass::ClientClass() {
		controller.client = this;

		pthread_mutex_init(&readMutex,NULL);
		pthread_mutex_init(&writeMutex,NULL);

		inReadQueue = false;
		inWriteQueue = false;

		if(time(&lastPackTime)== -1)
		{
			cout<<"can't get time"<<endl;
		}

		/*
		 * 写入创建成功消息，返回uid什么的
		 */
		char buff[100];
		sprintf(buff,"login success.your uid is:%d",user.uid);
		int len = strlen(buff);
		//std::cout<<"len is"<<len<<"content is"<<buff<<std::endl;
		lockWrite();
		writeBuff.insert(writeBuff.end(),buff,buff+len);
		unlockWrite();
	}

	ClientClass::~ClientClass() {
		pthread_mutex_destroy(&readMutex);
		pthread_mutex_destroy(&writeMutex);
	}

	int ClientClass::lockRead()
	{
		return pthread_mutex_lock(&readMutex);
	}
	int ClientClass::unlockRead()
	{
		return pthread_mutex_unlock(&readMutex);
	}

	int ClientClass::lockWrite()
	{
		return pthread_mutex_lock(&writeMutex);
	}

	int ClientClass::unlockWrite()
	{
		return pthread_mutex_unlock(&writeMutex);
	}

	bool ClientClass::canTick(time_t ctime)
	{
		if((ctime-lastPackTime) > Configs::instance().kickOutSeconds())
		{
			return true;
		}
		return false;
	}
}
